package minesweeper;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.io.File;
import java.io.IOException;

public class Cell extends MineButton{
	/**
	 * 
	 */
	//private static final long serialVersionUID = 4568055224002781945L;
    
	enum Status{
		FLAGGED("Flagged"),
		COVERED("Covered"),
		UNCOVERED("Uncovered");
		
		private Status(String name){ this.name = name; }
		public String getName(){ return name; }
		private String name;
	}
	public Cell(){
        xLocation = 0;
        yLocation = 0;
		value = 0;
		state = Status.COVERED;
		
		cI = new CoverImage();
		cI.setBounds(5, 5, 16, 16);
		add(cI, new Integer(2), 0);
		
	}
    public Cell(int x,int y){
        xLocation = x;
        yLocation = y;
		value = 0;
		state = Status.COVERED;
		
		cI = new CoverImage();
		cI.setBounds(5, 5, 16, 16);
		add(cI, new Integer(2), 0);
		
	}
    
    public Cell(String data){
        
        cI = new CoverImage();
        cI.setBounds(5, 5, 16, 16);
        add(cI, new Integer(2), 0);
        
        String[] newData = data.split(",");
        xLocation = Integer.parseInt(newData[0].substring(5, newData[0].length()));
        yLocation = Integer.parseInt(newData[1].substring(5, newData[1].length()));
        value = Integer.parseInt(newData[2].substring(6, newData[2].length()));
        state = Status.valueOf(newData[3].substring(6, newData[3].length()));
        cI.setCover(Integer.parseInt(newData[4].substring(6, newData[4].length())));
        mine = Boolean.parseBoolean(newData[5].substring(5, newData[5].length()));
        
        if(state == Status.FLAGGED){
            state = Status.FLAGGED;
            setEnabled(false);
            drawCell(CoverImage.FLAG);     
        } else if (state == Status.UNCOVERED){
            uncoverCell();
        }
    }
	public void drawCell(int graphic){
		cI.setCover(graphic);
	}
	public void SetFlag(){
		if(state == Status.FLAGGED){
			state = Status.COVERED;
			setEnabled(true);
			drawCell(0);
		} else if (state == Status.COVERED){
			state = Status.FLAGGED;
			setEnabled(false);
			setPressed(false);
			drawCell(CoverImage.FLAG);
		}
	}
	public boolean isMined(){
		return mine;
	}
	public void setValue(int value){
		this.value = value;
	}
	public int getValue(){
		return value;
	}
	
	public void setMine(){
		mine = true;
	}
	
	public Status getState(){
		return state;
	}
	
	public boolean isCovered(){
		if(state == Status.COVERED){
			return true;
		} else {
			return false;
		}
	}
	
	public void uncoverCell(){
      if(mine){
            drawCell(CoverImage.MINE);
        } else{
		    state = Status.UNCOVERED;
			setPressed(true);
			setEnabled(false);
			if(value > 0){
				drawCell(value);
			}
        }
	}

	public String toString(){
        return
            "xLoc=" + String.valueOf(xLocation) + 
            ",yLoc=" + String.valueOf(yLocation) + 
            ",value=" + String.valueOf(value) + 
            ",state=" + String.valueOf(state) +
            ",image=" + String.valueOf(cI.getCover()) +
            ",mine=" + String.valueOf(mine);
    }
	private boolean mine = false;
	private int value;
	private Status state;
	private CoverImage cI;
	
    
    public int xLocation;
    public int yLocation;
    
	public static final Status FLAGGED = Status.FLAGGED;
	public static final Status COVERED = Status.COVERED;
	public static final Status UNCOVERED = Status.UNCOVERED;
    
	public static void main(String[] args){
        Cell cell = new Cell(1, 1);
        cell.setMine();
        System.out.println(cell.toString());
        Cell cell2 = new Cell(cell.toString());
	}

}
/**
 * CoverImage is a means of controlling the top image component on top of the Cell class.
 * So use simply add CoverImage then change using setCover(Image)
 * @author Justin R. Hurst
 * @version v1.00
 */
class CoverImage extends JComponent{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5839142382588032266L;
	public CoverImage(){
		try{
			current = null;
			flag = ImageIO.read(new File("flag.png"));
			mine = ImageIO.read(new File("mine.png"));
			explosion = ImageIO.read(new File("explosion.png"));
			numberOne = ImageIO.read(new File("1.png"));
			numberTwo = ImageIO.read(new File("2.png"));
			numberThree = ImageIO.read(new File("3.png"));
			numberFour = ImageIO.read(new File("4.png"));
			numberFive = ImageIO.read(new File("5.png"));
			numberSix = ImageIO.read(new File("6.png"));
			numberSeven = ImageIO.read(new File("7.png"));
			numberEight = ImageIO.read(new File("8.png"));
		} catch(IOException e){
			e.printStackTrace();
		}
	}
	public void paintComponent(Graphics g){
		if(current == null) return;
		
		g.drawImage(current, 0,0,null);
        cInt = 0;
	}
	
	public void setCover(int Image){
        cInt = Image;
		if(Image == -3){
			current = explosion;
		}else if(Image == -2){
			current = mine;
		}else if(Image == -1){
			current = flag;
		} else if(Image == 1){
			current = numberOne;
		}else if(Image == 2){
			current = numberTwo;
		}else if(Image == 3){
			current = numberThree;
		}else if(Image == 4){
			current = numberFour;
		}else if(Image == 5){
			current = numberFive;
		}else if(Image == 6){
			current = numberSix;
		}else if(Image == 7){
			current = numberSeven;
		}else if(Image == 8){
			current = numberEight;
		} else {
			current = null;
		}
		repaint();
	}
    public int getCover(){
        return cInt;
    }
    
	public static final int ONE = 1;
	public static final int TWO = 2;
	public static final int THREE = 3;
	public static final int FOUR = 4;
	public static final int FIVE = 5;
	public static final int SIX = 6;
	public static final int SEVEN = 7;
	public static final int EIGHT = 8;
	public static final int FLAG = -1;
	public static final int MINE = -2;
	public static final int EXPLOSION = -3;
	
	private static Image mine;
	private Image current;
    private int cInt;
	private static Image flag;
	private static Image explosion;
	private static Image numberOne;
	private static Image numberTwo;
	private static Image numberThree;
	private static Image numberFour;
	private static Image numberFive;
	private static Image numberSix;
	private static Image numberSeven;
	private static Image numberEight;
}
